<?php
/**
 *User :门前一棵柿子树
 *Date:2024/4/15
 *Email:785313152@qq.com
 */

namespace app\admin\model;

use think\Model;

class Propose extends Model
{
    public function getLists()
    {
        $data = input('param.');    $limit = input('limit');
        $data['status']!==''?$where['status'] = $data['status']:'';
        $data['title'] && $where['title'] = ['like',"%{$data['title']}%"];
        input('complete_type')?$where['propsoe_type'] = input('complete_type'):'';


        if($data['begin_time']){
            $data['begin_time'] = explode(' ~ ',$data['begin_time']);
            $where['add_time'] = ['between',$data['begin_time']];
        }
        if($data['approval_time']){
            $data['approval_time'] = explode(' ~ ',$data['approval_time']);
            $where['class_approval_time'] = ['between',$data['approval_time']];
        }
        if($data['finishe_time']){
            $data['finishe_time'] = explode(' ~ ',$data['finishe_time']);
            $where['solution_time'] = ['between',$data['solution_time']];
        }

        $list = $this->where($where)->order('add_time desc')->paginate($limit)->toArray();
        $list['code'] = 0;//用于前台数据表格判断成功字段
        return $list;
    }

    public function output(){
        $data = input('param.');    $limit = input('limit');
        $data['status']!==''?$where['status'] = $data['status']:'';
        $data['title'] && $where['title'] = ['like',"%{$data['title']}%"];
        input('complete_type')?$where['propsoe_type'] = input('complete_type'):'';


        if($data['begin_time']){
            $data['begin_time'] = explode(' ~ ',$data['begin_time']);
            $where['add_time'] = ['between',$data['begin_time']];
        }
        if($data['approval_time']){
            $data['approval_time'] = explode(' ~ ',$data['approval_time']);
            $where['class_approval_time'] = ['between',$data['approval_time']];
        }
        if($data['finishe_time']){
            $data['finishe_time'] = explode(' ~ ',$data['finishe_time']);
            $where['solution_time'] = ['between',$data['solution_time']];
        }
        $departmentList = cache('department_list');
        $departmentList = array_column($departmentList['department'], 'name', 'id');
        $list = $this->where($where)->order('add_time desc')->select();
        $cate = ProposeClassify::column('name', 'id');
        $status = [
            0 => "已关闭",
            1 => "已发起",
            2 => "审核中",
            3 => "已通过",
            4 => "待分配",
            5 => "待执行",
            6 => "已完成"
        ];
        $right_type = [
            0=>'',
            1=>'专利',
            2=>"商业秘密"
        ];
        include_once('./public/phpexcel/PHPExcel.php');
        $PHPExcel = new \PHPExcel();
        //填入表头
        $PHPExcel->getActiveSheet()->setCellValue('A1', '序号');
        $PHPExcel->getActiveSheet()->setCellValue('B1', '标题');
        $PHPExcel->getActiveSheet()->setCellValue('C1', '提议部门');
        $PHPExcel->getActiveSheet()->setCellValue('D1', '提议人');
        $PHPExcel->getActiveSheet()->setCellValue('E1', '问题类型');
        $PHPExcel->getActiveSheet()->setCellValue('F1', '状态');
        $PHPExcel->getActiveSheet()->setCellValue('G1', '提议时间');
        $PHPExcel->getActiveSheet()->setCellValue('H1', '问题详情');
        $PHPExcel->getActiveSheet()->setCellValue('I1', '解决方案');
        $PHPExcel->getActiveSheet()->setCellValue('J1', '完成人员');
        $PHPExcel->getActiveSheet()->setCellValue('K1', '知识产权');
        $PHPExcel->getActiveSheet()->setCellValue('L1', '产权类型');
        $PHPExcel->getActiveSheet()->setCellValue('M1', '执行方案');

        if($list){
            $i=2;
            foreach ($list as $k=>$v){
                $PHPExcel->getActiveSheet()->setCellValue('A'.$i, $v['id']);
                $PHPExcel->getActiveSheet()->setCellValue('B'.$i, $v['title']);
                $PHPExcel->getActiveSheet()->setCellValue('C'.$i, $departmentList[$v['user_department']]);
                $PHPExcel->getActiveSheet()->setCellValue('D'.$i, $v['create_user_info']['name']);
                $PHPExcel->getActiveSheet()->setCellValue('E'.$i, $cate[$v['propsoe_type']]);
                $PHPExcel->getActiveSheet()->setCellValue('F'.$i, $status[$v['status']]);
                $PHPExcel->getActiveSheet()->setCellValue('G'.$i, $v['add_time']);
                $PHPExcel->getActiveSheet()->setCellValue('H'.$i, $v['questiont']);
                $PHPExcel->getActiveSheet()->setCellValue('I'.$i, $v['question_soluton']);
                $PHPExcel->getActiveSheet()->setCellValue('J'.$i, $v['solution_user_info']['name']);
                $PHPExcel->getActiveSheet()->setCellValue('K'.$i, $v['is_copyright']==1?"是":"否");
                if($v['is_copyright'] ==1) {
                    $PHPExcel->getActiveSheet()->setCellValue('L' . $i, $right_type[$v['copyright_type']]);
                }else{
                    $PHPExcel->getActiveSheet()->setCellValue('L' . $i, '');
                }

                $PHPExcel->getActiveSheet()->setCellValue('M'.$i, $v['solution_content']);
                $i++;
            }
            //设置单元格宽度
            $PHPExcel->getActiveSheet()->getColumnDimension('k')->setWidth(20);
            $objWriter = new \PHPExcel_Writer_Excel2007($PHPExcel);
            header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
            header("Content-Type:application/force-download");
            header("Content-Type:application/vnd.ms-execl");
            header("Content-Type:application/octet-stream");
            header("Content-Type:application/download");
            header('Content-Disposition:attachment;filename="'.date('YmdHis').'.xlsx"');
            header("Content-Transfer-Encoding:binary");
            $objWriter->save('php://output');
        }else{
            exit("<script>alert('当前结果为空');history.go(-1);</script>");
        }
    }


    public function setCreateUserInfoAttr($value)
    {
        if ($value) {
            return json_encode($value, 256);
        }
        return '';
    }

    public function getCreateUserInfoAttr($value)
    {
        if ($value) {
            return json_decode($value, true);
        }
        return [];
    }

    public function setLeaderUserInfoAttr($value)
    {
        if ($value) {
            return json_encode($value, 256);
        }
        return '';
    }

    public function getLeaderUserInfoAttr($value)
    {
        if ($value) {
            return json_decode($value, true);
        }
        return [];
    }

    public function setClassUserInfoAttr($value)
    {
        if ($value) {
            return json_encode($value, 256);
        }
        return '';
    }

    public function getClassUserInfoAttr($value)
    {
        if ($value) {
            return json_decode($value, true);
        }
        return [];
    }

    public function setHrUserInfoAttr($value)
    {
        if ($value) {
            return json_encode($value, 256);
        }
        return '';
    }

    public function getHrUserInfoAttr($value)
    {
        if ($value) {
            return json_decode($value, true);
        }
        return [];
    }

    public function setSolutionUserInfoAttr($value)
    {
        if ($value) {
            return json_encode($value, 256);
        }
        return '';
    }

    public function getSolutionUserInfoAttr($value)
    {
        if ($value) {
            return json_decode($value, true);
        }
        return [];
    }

}